//
//  TBCRadioViewController.m
//  待定
//
//  Created by BoyaLee on 15/11/5.
//  Copyright © 2015年 BoyaLee. All rights reserved.
//

#import <MJRefresh.h>
#import "TBCRadioViewController.h"
#import "TBCRadioTableViewCell.h"
#import "TBCRadioThreeImageTableViewCell.h"
#import "TBCRadioListModel.h"
#import "TBCRadioStationViewController.h"
#import "TBCPlayerViewController.h"
#import "TBCCarouselModel.h"
#import "XLImagePageView.h"

@interface TBCRadioViewController ()<UITableViewDelegate, UITableViewDataSource, TBCRadioThreeImageTableViewCellDelegate>

@property (nonatomic, strong) BaseTableView *myTableV;

@property (nonatomic, strong) NSMutableArray *allRadioArray;

@property (nonatomic, strong) NSMutableArray *allHotStationArray;

@property (nonatomic, strong) XLImagePageView *carouselV;

@property (nonatomic, assign) NSInteger dataIndex;

@property (nonatomic, assign) BOOL firstIn;

@end

@implementation TBCRadioViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([[[TBCPlayerViewController shareRadioPlayerViewController] player] state] == STKAudioPlayerStatePlaying || [[[TBCPlayerViewController shareRadioPlayerViewController] player] state] == STKAudioPlayerStatePaused || [[[TBCPlayerViewController shareRadioPlayerViewController] player] state] == STKAudioPlayerStateBuffering) {
        self.navigationItem.rightBarButtonItem.enabled = YES;
        UIImage *image = [UIImage imageNamed:@"player_yes.png"];
        image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        self.navigationItem.rightBarButtonItem.image = image;
    }
    else{
        self.navigationItem.rightBarButtonItem.enabled = NO;
        UIImage *image = [UIImage imageNamed:@"player_no.png"];
        image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        self.navigationItem.rightBarButtonItem.image = image;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.firstIn = YES;
    
    self.allRadioArray = [NSMutableArray array];
    self.allHotStationArray = [NSMutableArray array];
    
    self.carouselV = [[XLImagePageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT * 0.25)];
    [self.view addSubview:_carouselV];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"player_no.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonAction)];
    
    self.myTableV = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    _myTableV.delegate = self;
    _myTableV.dataSource = self;
    _myTableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    _myTableV.tableHeaderView = _carouselV;
    [self.view addSubview:self.myTableV];
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getHeaderData)];
    _myTableV.header = header;
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getFooterData)];
    _myTableV.footer = footer;
    [_myTableV.header beginRefreshing];
}

- (void)getHeaderData
{
    self.dataIndex = 0;
    if (_firstIn) {
        [self getRadioList];
    } else {
        [self refreshDate];
    }
    self.firstIn = NO;
}

- (void)getFooterData
{
    self.dataIndex += 9;
    [self refreshDate];
}

- (void)rightBarButtonAction
{
    [self.navigationController pushViewController:[TBCPlayerViewController shareRadioPlayerViewController] animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _allRadioArray.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.row) {
        TBCRadioThreeImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ThreeCell"];
        if (!cell) {
            cell = [[TBCRadioThreeImageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ThreeCell"];
        }
        cell.allHotStationArray = _allHotStationArray;
        cell.delegate = self;
        
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
        
    } else {
        TBCRadioTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[TBCRadioTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        cell.radioListModel = [_allRadioArray objectAtIndex:indexPath.row - 1];
        
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
}

- (void)pushToRadioStation:(TBCRadioListModel *)radio
{
    TBCRadioStationViewController *radioStation = [[TBCRadioStationViewController alloc] init];
    radioStation.title = radio.title;
    radioStation.radio = radio;
    [self.navigationController pushViewController:radioStation animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    TBCRadioStationViewController *radioStationVC = [[TBCRadioStationViewController alloc] init];
    TBCRadioListModel *radio = [_allRadioArray objectAtIndex:indexPath.row - 1];
    radioStationVC.radio = radio;
    radioStationVC.navigationItem.title = radio.title;
    [self.navigationController pushViewController:radioStationVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.row) {
        return HEIGHT * 0.2;
    } else {
        return HEIGHT * 0.15;
    }
}

- (void)getRadioList
{
    NSString *url_str = @"http://api2.pianke.me/ting/radio";
    NSDictionary *para = @{@"auth":@"", @"client":@"1", @"deviceid":@"48CA5912-65D6-477A-8D4D-4015E5C59B9C&version=3.0.6"};
    NSString *cookie = @"PHPSESSID=rmkoq24lvekmcplf9jal2odpm6";
    
    [TBCAppTools getDatawithPost:url_str paramenters:para cookie:cookie Block:^(id responseObject) {
        
        NSDictionary *dic = responseObject;
        NSDictionary *dataDic = [dic objectForKey:@"data"];
        
        // 轮播图数据解析
        NSMutableArray *imageUrlArray = [NSMutableArray arrayWithCapacity:0];
        NSArray *carouselArray = [dataDic objectForKey:@"carousel"];
        for (NSDictionary *dic in carouselArray) {
            TBCCarouselModel *carousel = [[TBCCarouselModel alloc] init];
            [carousel setValuesForKeysWithDictionary:dic];
//            NSString *tingid = [carousel.url substringFromIndex:13];
//            [_allCarouselTingidArray addObject:tingid];
            
            [imageUrlArray addObject:[dic objectForKey:@"img"]];
        }
        
        _carouselV.imageArray = imageUrlArray;
        
        // hotStation解析
        NSArray *hotlistArray = [dataDic objectForKey:@"hotlist"];
        
        for (NSDictionary *dic in hotlistArray) {
            TBCRadioListModel *radio = [[TBCRadioListModel alloc] init];
            [radio setValuesForKeysWithDictionary:dic];
            radio.userModel = [[TBCUserModel alloc] init];
            [radio.userModel setValuesForKeysWithDictionary:[dic objectForKey:@"userinfo"]];
            [_allHotStationArray addObject:radio];
        }
        
        // list解析
        NSArray *alllist = [dataDic objectForKey:@"alllist"];
        for (NSDictionary *dic in alllist) {
            TBCRadioListModel *radioListModel = [[TBCRadioListModel alloc] init];
            [radioListModel setValuesForKeysWithDictionary:dic];
            radioListModel.userModel = [[TBCUserModel alloc] init];
            [radioListModel.userModel setValuesForKeysWithDictionary:[dic objectForKey:@"userinfo"]];
            
            [self.allRadioArray addObject:radioListModel];
        }
        [_myTableV reloadData];
        [_myTableV.header endRefreshing];
        [_myTableV.footer endRefreshing];
    }];
}

-(void)refreshDate{
    
    NSString *url_string = @"http://api2.pianke.me/ting/radio_list";
    NSDictionary *para = @{@"client":@"1",@"auth":@"C807FCuwm36lDC8l3HMTslDJxQMKpNLeNoj1vCGMzcWP22NZLTRhY4fI",@"deviceid":@"63900960-1ED5-47D0-B561-86C09F41ED08",@"limit":@"9",@"start":[NSString stringWithFormat:@"%ld",_dataIndex],@"version":@"3.0.6"};
    NSString *cookie = @"PHPSESSID=dlbvjmr0e59672q3lseu1fomu5";
    
    [TBCAppTools getDatawithPost:url_string paramenters:para cookie:cookie Block:^(id responseObject) {
        
        NSDictionary *dic = responseObject;
        
        //  解析的是 alllist
        NSArray *arrayAlllist = [[dic objectForKey:@"data"] objectForKey:@"list"];
        for(NSDictionary *dictionary in arrayAlllist){
            TBCRadioListModel *radioModel = [[TBCRadioListModel alloc] init];
            [radioModel setValuesForKeysWithDictionary:dictionary];
            radioModel.userModel = [[TBCUserModel alloc] init];
            radioModel.userModel.uname = [[dictionary objectForKey:@"userinfo"] objectForKey:@"uname"];
            [_allRadioArray addObject:radioModel];
        }
        
        [_myTableV.header endRefreshing];
        [_myTableV.footer endRefreshing];
        [_myTableV reloadData];
    }];
}

//- (void)XLImagePageView:(XLImagePageView *)imagePageView didSelectPageAtIndex:(NSInteger)pageIndex
//{
//    TBCPlayerViewController *playerVC = [[TBCPlayerViewController alloc] init];
//    NSString *tingid = [_allCarouselTingidArray objectAtIndex:pageIndex];
//    NSLog(@"%@", tingid);
//    [self getCarouseDetailData:tingid];
//    playerVC.radio = _carouseRadioModel;
//    
//    [self.navigationController pushViewController:playerVC animated:YES];
//}

//- (void)getCarouseDetailData:(NSString *)tingid
//{
//    NSString *url_str = @"http://api2.pianke.me/ting/info";
//    NSDictionary *para = @{@"auth":@"", @"client":@"1", @"deviceid":@"48CA5912-65D6-477A-8D4D-4015E5C59B9C", @"tingid":tingid, @"version":@"3.0.6"};
//   NSString *cookie = @"PHPSESSID=kpgdatkm2jdphcfh2de44q3pb6";
//    
//    [TBCAppTools getDatawithPost:url_str paramenters:para cookie:nil Block:^(id responseObject) {
//        
//        NSDictionary *resultDic = responseObject;
//        NSDictionary *playInfoDic = [[resultDic objectForKey:@"data"] objectForKey:@"playinfo"];
//        TBCRadioListModel *radio = [[TBCRadioListModel alloc] init];
//        radio.title = [playInfoDic objectForKey:@"title"];
//        radio.coverimg = [playInfoDic objectForKey:@"imgurl"];
//        radio.musicUrl = [playInfoDic objectForKey:@"musicUrl"];
//        
//        self.carouseRadioModel = radio;
//    }];
//}

@end
